from faker import Faker
import random
import mysql.connector
from common import snowFaker as sf
from datetime import timedelta

# 初始化Faker实例
fake = Faker('zh_CN')  # 使用中文区域生成中文姓名

# 连接到数据库
conn = mysql.connector.connect(
    host='localhost',
    user='root',
    password='123456',
    database='esl'
)

cursor = conn.cursor()


# 生成虚拟数据
def generate_fake_user():
    id = sf.get_id()
    user_id = fake.uuid4().replace('-', '')
    user_name = fake.name()  # 中文姓名
    age = random.randint(10, 90)
    gender = random.choice(['13', '14'])  # 13代表男性，14代表女性
    phone = '1' + ''.join([random.choice('3456789') for _ in range(10)])  # 电话格式：11位数字，且以1开头
    email = f'{id}@example.com'
    avatar = 'img.png'
    password = '123456'

    # 生成一段自我介绍
    introduction = f"大家好，我叫{user_name}，今年{age}岁，喜欢美食、运动以及旅行，期待和大家一起分享有趣的经历！"

    create_time = fake.date_this_decade()
    # recent_login_time确保大于create_time
    recent_login_time = create_time + timedelta(days=random.randint(1, 365))  # recent_login_time比create_time大

    return (
    id, user_id, user_name, age, gender, phone, email, avatar, password, introduction, create_time, recent_login_time)


# 插入虚拟数据到数据库
def insert_fake_user():
    user_data = generate_fake_user()
    query = """INSERT INTO user 
               (id, user_id, user_name, age, gender, phone, email, avatar, password, introduction, create_time, recent_login_time) 
               VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"""
    cursor.execute(query, user_data)
    conn.commit()


# 生成并插入虚拟数据
for i in range(500000):
    insert_fake_user()
    # print(f"已插入第{i + 1}条虚拟数据")

# 关闭连接
cursor.close()
conn.close()

print("虚拟数据已全部成功生成并插入数据库！")
